package cn.zucc.edu.handyzucc.service.impl;

import cn.zucc.edu.handyzucc.dao.ClassDao;
import cn.zucc.edu.handyzucc.dao.DormDao;
import cn.zucc.edu.handyzucc.dao.UserDao;
import cn.zucc.edu.handyzucc.entity.User;
import cn.zucc.edu.handyzucc.service.ImageService;
import cn.zucc.edu.handyzucc.service.UserService;
import cn.zucc.edu.handyzucc.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private DormDao dormDao;
    @Autowired
    private ClassDao classDao;
    @Autowired
    private ImageService imageService;

    @Override
    public boolean signup(User user) {
        if ((user.getUser_username() != null && !"".equals(user.getUser_username()))
                || (user.getUser_password() != null && !"".equals(user.getUser_password()))) {
            try {
                int effectedNum = userDao.signup(user);
                if (effectedNum > 0)
                    return true;
                else throw new RuntimeException("注册失败");
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        } else throw new RuntimeException("信息不能为空");
    }

    @Override
    public User login(String user_username, String user_password) {
        if ((user_username != null && !"".equals(user_username))
                || (user_password != null && !"".equals(user_password))) {
            try {
                User user1 = userDao.login(user_username, user_password);
                if (user1 != null)
                    return user1;
                else throw new RuntimeException("登录失败");
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        } else throw new RuntimeException("信息不能为空");
    }

    @Override
    public User getAuthorById(Integer user_id) {
        return userDao.getAuthorById(user_id);
    }

    @Override
    public User getUserById(Integer user_id) {
        return userDao.getUserById(user_id);
    }

    @Override
    public List<User> getFollow(Integer follow_follower) {
        return userDao.getFollow(follow_follower);
    }

    @Override
    public List<User> getFans(Integer follow_leader) {
        return userDao.getFans(follow_leader);
    }

    @Override
    public List<User> getUsersByKeyword(String keyword) {
        return userDao.getUsersByKeyword(keyword);
    }

    @Override
    public boolean updateAvator(Integer user_id, Integer user_avator) {
        return userDao.updateAvator(user_id, user_avator) == 1;
    }

    @Override
    public List<User> getAllStudent() {
        return userDao.getAllStudent();
    }

    @Override
    public boolean deleteStudent(String user_username) {
        return userDao.deleteStudent(user_username) == 1;
    }

    @Override
    public boolean updateStudent(User user) {
        return userDao.updateStudent(user) == 1;
    }

    @Override
    public List<User> batchSignup(MultipartFile file) throws Exception {
        List<User> users = new ArrayList<>();
        try {
            InputStream inputStream = file.getInputStream();
            List<List<Object>> list = ExcelUtils.getStudentListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();

            for (int i = 0; i < list.size(); i++) {
                List<Object> studentList = list.get(i);

                User user = new User();
                user.setUser_username(studentList.get(0).toString());
                if (userDao.getStudentByUsername(user.getUser_username()) != null)
                    continue;

                user.setUser_password(studentList.get(1).toString());
                user.setUser_truename(studentList.get(2).toString());
                user.setUser_nickname(studentList.get(3).toString());

                user.setUser_dorm(Integer.parseInt(studentList.get(4).toString()));
                if (dormDao.getDormById(user.getUser_dorm()) == null)
                    continue;

                user.setUser_dorm_building(studentList.get(5).toString());
                user.setUser_dorm_room(studentList.get(6).toString());

                user.setUser_class(Integer.parseInt(studentList.get(7).toString()));
                if (classDao.getClassById(user.getUser_class()) == null)
                    continue;

                user.setUser_class_college(studentList.get(8).toString());
                user.setUser_class_major(studentList.get(9).toString());
                user.setUser_class_number(studentList.get(10).toString());
                user.setUser_email(studentList.get(11).toString());
                user.setUser_phone(studentList.get(12).toString());
                userDao.signup(user);
                System.out.println(user.getUser_id());
                imageService.uploadAvatorStream(getClass().getClassLoader().getResourceAsStream("zuccicon.jpg"), user.getUser_id());
//                FileInputStream avator = new FileInputStream("./src/main/resources/static/zuccicon.jpg");
//                imageService.uploadAvatorStream(avator, user.getUser_id());
//                avator.close();
                users.add(user);
            }
        } catch (Exception e) {

        }
        return users;
    }

    @Override
    public boolean updateInfo(String type, String newValue, int user_id) {
        return userDao.updateInfo(type, newValue, user_id) == 1;
    }

    @Override
    public boolean updatePassword(String user_username, String user_password) {
        return userDao.updatePassword(user_username, user_password) == 1;
    }

    @Override
    public boolean resetPassword(String user_username) {
        return userDao.resetPassword(user_username) == 1;
    }

    @Override
    public boolean resetPasswordUser(String user_username, String user_truename, String user_phone) {
        return userDao.resetPasswordUser(user_username, user_truename, user_phone) == 1;
    }
}
